import * as vscode from 'vscode';
import {authorizationVerification, getAccessToken, getCompletion} from "../authorize";

import { getCompletionItems as getLuaKeywordCompletionItems } from './functions/client/luaKeywords';
import { getCompletionItems as getLuaGlobalCompletionItems } from './functions/client/luaGlobals';
import { getCompletionItems as getSLFunctionCompletionItems } from './functions/client/slFunctions';
import { getCompletionItems as getCodeBlockCompletionItems } from './functions/client/codeBlockFunctions';
import { getCompletionItems as getMetaValueCompletionItems } from './functions/client/metaValueFunctions';
import { getCompletionItems as getLuaEventCompletionItems } from './functions/client/luaEventFunctions';
import { getCompletionItems as getRegisterTriggerCompletionItems } from './functions/client/registerTriggerFunctions';
import { getCompletionItems as getNetMsgCompletionItems } from './functions/client/netMsgFunctions';
import { getCompletionItems as getActiveCompletionItems } from './functions/client/activeFunctions';
import { getCompletionItems as getWinCompletionItems } from './functions/client/winFunctions';
import { getCompletionItems as getCommonFunCompletionItems } from './functions/client/commonFunFunctions';
import { getCompletionItems as getImageCompletionItems } from './functions/client/imageFunctions';
import { getCompletionItems as getButtonCompletionItems } from './functions/client/buttonFunctions';
import { getCompletionItems as getTextCompletionItems } from './functions/client/textFunctions';
import { getCompletionItems as getBmpTextCompletionItems } from './functions/client/bmpTextFunctions';
import { getCompletionItems as getTextAtlasCompletionItems } from './functions/client/textAtlasFunctions';
import { getCompletionItems as getRichTextCompletionItems } from './functions/client/richTextFunctions';
import { getCompletionItems as getScrollTextCompletionItems } from './functions/client/scrollTextFunctions';

import { getCompletionItems as getNodeCompletionItems } from './functions/client/nodeFunctions';
import { getCompletionItems as getWidgetCompletionItems } from './functions/client/widgetFunctions';
import { getCompletionItems as getItemShowCompletionItems } from './functions/client/itemShowFunctions';
import { getCompletionItems as getItemBoxCompletionItems } from './functions/client/itemBoxFunctions';
import { getCompletionItems as getCheckBoxCompletionItems } from './functions/client/checkBoxFunctions';
import { getCompletionItems as getTextInputCompletionItems } from './functions/client/textInputFunctions';
import { getCompletionItems as getSliderCompletionItems } from './functions/client/sliderFunctions';
import { getCompletionItems as getProgressTimerCompletionItems } from './functions/client/progressTimerFunctions';
import { getCompletionItems as getLoadingBarCompletionItems } from './functions/client/loadingBarFunctions';
import { getCompletionItems as getEffectCompletionItems } from './functions/client/effectFunctions';
import { getCompletionItems as getTimelineCompletionItems } from './functions/client/timelineFunctions';
import { getCompletionItems as getUIModelCompletionItems } from './functions/client/uiModelFunctions';
import { getCompletionItems as getLayoutCompletionItems } from './functions/client/layoutFunctions';
import { getCompletionItems as getListViewCompletionItems } from './functions/client/listViewFunctions';
import { getCompletionItems as getScrollViewCompletionItems } from './functions/client/scrollViewFunctions';
import { getCompletionItems as getPageViewCompletionItems } from './functions/client/pageViewFunctions';
import { getCompletionItems as getQuickCellCompletionItems } from './functions/client/quickCellFunctions';
import { getCompletionItems as getActionFunCompletionItems } from './functions/client/actionFunFunctions';
import { getCompletionItems as getFramesCompletionItems } from './functions/client/framesFunctions';
import { getCompletionItems as getParticleEffectCompletionItems } from './functions/client/particleEffectFunctions';
import { getCompletionItems as getSpineAnimCompletionItems } from './functions/client/spineAnimFunctions';
import { getCompletionItems as getMoveWidgetCompletionItems } from './functions/client/moveWidgetFunctions';
import { getCompletionItems as getScrapePicCompletionItems } from './functions/client/scrapePicFunctions';
import { getCompletionItems as getTableViewCompletionItems } from './functions/client/tableViewFunctions';
import { getCompletionItems as getRotateViewCompletionItems } from './functions/client/rotateViewFunctions';
import { getCompletionItems as getEquipShowCompletionItems } from './functions/client/equipShowFunctions';


export function getAllCompletionItems(): vscode.CompletionItem[]{
    return [
        ...getLuaKeywordCompletionItems(),
        ...getLuaGlobalCompletionItems(),
        ...getSLFunctionCompletionItems(),
        ...getCodeBlockCompletionItems(),
        ...getMetaValueCompletionItems(),
        ...getLuaEventCompletionItems(),
        ...getRegisterTriggerCompletionItems(),
        ...getNetMsgCompletionItems(),
        ...getActiveCompletionItems(),
        ...getWinCompletionItems(),
        ...getCommonFunCompletionItems(),
        ...getImageCompletionItems(),
        ...getButtonCompletionItems(),
        ...getTextCompletionItems(),
        ...getBmpTextCompletionItems(),
        ...getTextAtlasCompletionItems(),
        ...getRichTextCompletionItems(),
        ...getScrollTextCompletionItems(),
        ...getNodeCompletionItems(),
        ...getWidgetCompletionItems(),

        ...getItemShowCompletionItems(),
        ...getItemBoxCompletionItems(),
        ...getCheckBoxCompletionItems(),
        ...getTextInputCompletionItems(),
        ...getSliderCompletionItems(),
        ...getProgressTimerCompletionItems(),
        ...getLoadingBarCompletionItems(),
        ...getEffectCompletionItems(),
        ...getTimelineCompletionItems(),
        ...getUIModelCompletionItems(),
        ...getLayoutCompletionItems(),
        ...getListViewCompletionItems(),
        ...getScrollViewCompletionItems(),
        ...getPageViewCompletionItems(),
        ...getQuickCellCompletionItems(),
        ...getActionFunCompletionItems(),
        ...getFramesCompletionItems(),
        ...getParticleEffectCompletionItems(),
        ...getSpineAnimCompletionItems(),
        ...getMoveWidgetCompletionItems(),
        ...getScrapePicCompletionItems(),
        ...getTableViewCompletionItems(),
        ...getRotateViewCompletionItems(),
        ...getEquipShowCompletionItems(),
    ];
}

export function createLuaCompletionProvider(context: vscode.ExtensionContext) {

    context.subscriptions.push(vscode.languages.registerCompletionItemProvider(
            { language: 'lua' },
            {
                async provideCompletionItems(document: vscode.TextDocument, position: vscode.Position) {

                    if (getCompletion(context) === 'false'){
                        return;
                    }

                    const isAuthorized = await authorizationVerification(context);
                    if (!isAuthorized) {
                        return [];
                    }

                    return getAllCompletionItems();
                }
            },
            ':' // Trigger completion when '.' is typed
        )
    );
}
